package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.breadtrip.R;

/**
 * Created by sunsg on 16-4-11.
 * 弧度的计算公式为： 2*PI/360*角度；
 */
public class WatchView extends View {
    private float mBorderWidth;
    private int mBorderColor;

    private Paint mPaint;
    private RectF mBounds;
    private float mWidth;
    private float mHeight;
    private float mRadius;

    private int mSmalLength;
    private int mLargerLength;

    public WatchView(Context context) {
        super(context);
        init(context);
    }

    public WatchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WatchView);
        try {
            mBorderWidth = typedArray.getDimension(R.styleable.WatchView_border_width, 2);
            mBorderColor = typedArray.getColor(R.styleable.WatchView_border_color, 0XFF000000);
        } finally {
            typedArray.recycle();
        }
        init(context);
    }

    public WatchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setColor(mBorderColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mBounds = new RectF(getLeft(), getTop(), getRight(), getBottom());
        mWidth = mBounds.right - mBounds.left;
        mHeight = mBounds.bottom - mBounds.top;

        if (mWidth < mHeight) {
            mRadius = mWidth / 4;
        } else {
            mRadius = mHeight / 4;
        }
        mSmalLength = 10;
        mLargerLength = 20;
        Log.i("test","onSizeChange");

    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(0xff000000);
//        mPaint.setColor(0x66555555);
        canvas.drawRoundRect(new RectF(mBounds.centerX() - (float) 0.9 * mWidth / 2, mBounds.centerY() - (float) 0.9 * mHeight / 2, mBounds.centerX() + (float) 0.9 * mWidth / 2, mBounds.centerY() + (float) 0.9 * mHeight / 2), 30, 30, mPaint);
        mPaint.setColor(mBorderColor);
        canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), mRadius, mPaint);
        float start_x, start_y;
        float end_x, end_y;
        for (int i = 0; i < 60; ++i) {
            start_x = mRadius * (float) Math.sin(Math.PI / 180 * i * 6);
            start_y = mRadius * (float) Math.cos(Math.PI / 180 * i * 6);
            if (i % 5 == 0) {
                end_x = start_x + mLargerLength * (float) Math.sin(Math.PI / 180 * i * 6);
                end_y = start_y + mLargerLength * (float) Math.cos(Math.PI / 180 * i * 6);
            } else {
                end_x = start_x + mSmalLength * (float) Math.sin(Math.PI / 180 * i * 6);
                end_y = start_y + mSmalLength * (float) Math.cos(Math.PI / 180 * i * 6);
            }
            start_x += mBounds.centerX();
            end_x += mBounds.centerX();
            start_y += mBounds.centerY();
            end_y += mBounds.centerY();
            canvas.drawLine(start_x, start_y, end_x, end_y, mPaint);
        }
        canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), 20, mPaint);

        //type1
//        canvas.drawLine(mBounds.centerX(), mBounds.centerY(), (float) (mBounds.centerX() + Math.sin(Math.PI/180 * 60)* mRadius), (float) (mBounds.centerY() - Math.cos(Math.PI/180 * 60)* mRadius), mPaint);
        //type 2
        canvas.rotate(60, mBounds.centerX(), mBounds.centerY());
        canvas.drawLine(mBounds.centerX(), mBounds.centerY(), mBounds.centerX(), mBounds.centerY() -mRadius, mPaint);


    }
}
